<template>
  <el-dialog
    :visible="isShow"
    title="新增员工"
    width="500px"
    @close="closeEvent"
  >
    <el-form label-width="120px" :model="form" :rules="rules" ref="form">
      <el-form-item label="姓名" prop="username">
        <el-input placeholder="请输入" v-model="form.username"></el-input>
      </el-form-item>
      <el-form-item label="手机" prop="mobile">
        <el-input placeholder="请输入" v-model="form.mobile"></el-input>
      </el-form-item>
      <el-form-item label="入职时间" prop="timeOfEntry">
        <el-date-picker type="date" v-model="form.timeOfEntry"></el-date-picker>
      </el-form-item>
      <el-form-item label="聘用形势" prop="formOfEmployment">
        <el-select placeholder="请输入" v-model="form.formOfEmployment">
          <el-option
            v-for="item in employees.hireType"
            :key="item.id"
            :label="item.value"
            :value="item.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="工号" prop="workNumber">
        <el-input placeholder="请输入" v-model="form.workNumber"></el-input>
      </el-form-item>
      <el-form-item label="部门" prop="departmentName">
        <!-- <span v-clickoutside="callbackFn"> -->
        <span v-clickoutside="callbackFn">
          <el-input
            readonly
            placeholder="请输入"
            v-model="form.departmentName"
            @focus="focusEvent"
          ></el-input>
          <!-- 组织架构
        el-tree
            data
            props:{label:"label",children:'children'}
        
         -->
          <el-tree
            :data="treeData"
            v-if="showTree"
            @node-click="nodeClick"
            :props="{ label: 'name' }"
          ></el-tree>
        </span>
      </el-form-item>
      <el-form-item label="转正时间" prop="correctionTime">
        <el-date-picker
          type="date"
          v-model="form.correctionTime"
        ></el-date-picker>
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="footer">
        <el-button type="primary" @click="submit">提交</el-button>
        <el-button @click="closeEvent">取消</el-button>
      </div>
    </template>
  </el-dialog>
</template>
<script>
// import的东西html不能直接使用
import employees from '@/assets/constant/employees.js'
import { companyDepartment } from '@/api/departments.js'
import { sysUserPost } from '@/api/employees.js'
export default {
  name: 'Dialog',
  props: {
    isShow: {
      default: false,
      type: Boolean
    }
  },
  data () {
    return {
      showTree: false,
      treeData: [], // 组织架构数据
      employees: employees,
      form: {
        username: '', //	string	非必须		姓名
        mobile: '', //	string	非必须		手机号
        timeOfEntry: '', //	string	非必须		入职时间
        formOfEmployment: '', //	number	非必须		聘用形式
        workNumber: '', //	string	非必须		工号
        departmentName: '', //	string	非必须		组织名称
        correctionTime: '' //	string	非必须		转正时间
      },
      rules: {
        username: [{ required: true, message: '必填', trigger: 'change' }], //	string	非必须		姓名
        mobile: [{ required: true, message: '必填', trigger: 'change' }], //	string	非必须		手机号
        timeOfEntry: [{ required: true, message: '必填', trigger: 'change' }], //	string	非必须		入职时间
        formOfEmployment: [
          { required: true, message: '必填', trigger: 'change' }
        ], //	number	非必须		聘用形式
        workNumber: [{ required: true, message: '必填', trigger: 'change' }], //	string	非必须		工号
        departmentName: [
          { required: true, message: '必填', trigger: 'change' }
        ], //	string	非必须		组织名称
        correctionTime: [{ required: true, message: '必填', trigger: 'change' }] //	string	非必须		转正时间
      }
    }
  },
  mounted () {
    // document.addEventListener('click', this.clickEvent)
  },
  methods: {
    closeEvent () {
      this.$emit('update:isShow', false)
    },
    submit () {
      this.$refs.form.validate(async result => {
        if (result) {
          await sysUserPost(this.form)
          this.$message.success('新增成功')
          this.closeEvent()
          //   刷新父级列表数据
          this.$emit('getData')
          console.log(result)
        }
      })
    },
    async focusEvent () {
      // 1:显示el-tree 2:加载接数据
      this.showTree = true
      if (this.treeData.length === 0) {
        const res = await companyDepartment()
        this.treeData = this.changeData(res.data.depts)
        console.log(res)
      }
    },
    // 树形数据转换
    changeData (arr, str = '') {
      return arr.filter(item => {
        if (item.pid == str) {
          item.children = this.changeData(arr, item.id)
          return true
        }
      })
    },
    // 树形组件点击
    nodeClick (data) {
      this.form.departmentName = data.name
      console.log(data)
    },
    clickEvent (e) {
      // 点击的dom是否属于el-tree,如果属于，不理，不属于关闭
      //   this.showTree = false
      //   this.$refs.treeArea===不需要理的dom区域
      //   e.target:点击的区域
      //    this.$refs.treeArea是否包含e.target
      if (this.isShow && !this.$refs.treeArea.contains(e.target)) {
        this.showTree = false
      }
    },
    callbackFn () {
      this.showTree = false
    }
  },
  beforeDestroy () {
    // document.removeEventListener('click', this.clickEvent)
  }
}
</script>
<style lang="scss" scoped>
.footer {
  text-align: center;
}
</style>
